package com.shop.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.mall.common.Result;
import com.shop.mall.entity.Admin;
import com.shop.mall.entity.Admin;
import com.shop.mall.entity.Admin;
import com.shop.mall.mapper.AdminMapper;
import com.shop.mall.service.AdminService;
import com.shop.mall.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 商家 服务实现类
 * </p>
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    /**
     * 根据登录名查询商家
     * @param loginName   登录名
     * @return   Admin
     */
    @Override
    public Admin findByLoginName(String loginName) {
        LambdaQueryWrapper<Admin> adminWrapper = new LambdaQueryWrapper<>();
        adminWrapper.eq(Admin::getLoginName, loginName);
        return adminMapper.selectOne(adminWrapper);
    }

    @Override
    public Admin getAdminById(int id) {
        return adminMapper.selectById(id);
    }

    /**
     * 查询所有商家信息
     * @return List<Admin>
     */
    @Override
    public List<Admin> getAdminList() {
        return adminMapper.selectList(null);
    }

    /**
     *  分页查询商家信息
     * @param pageSize 每页显示的条数
     * @param currentPage  要查询的页
     * @param name  商家姓名
     * @return IPage<Admin>
     */
    @Override
    public IPage<Admin> pageList(int pageSize, int currentPage, String name) {
        // like 条件构造
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name != null && !"".equals(name), Admin::getName, name);
        // 分页构造
        IPage<Admin> page = new Page<>(currentPage, pageSize);
        adminMapper.selectPage(page, wrapper);
        return page;
    }

    @Override
    public Result<String> add(Admin admin) {
        // 登录名校验
        if (admin.getLoginName() == null || "".equals(admin.getLoginName())) {
            return Result.error("登录名不能为空");
        }
        // 登录名重复校验
        if (findByLoginName(admin.getLoginName()) != null) {
            return Result.error("登录名已存在，请重新输入");
        }
        if (admin.getName() == null || "".equals(admin.getName())) {
            return Result.error("姓名不能为空");
        }
        admin.setPassword(Md5Util.getMD5String("123456"));
        if (adminMapper.insert(admin) > 0) {
            return Result.success("添加成功");
        }
        return Result.error("添加失败");
    }

    @Override
    public boolean updateAdmin(Admin admin) {
        LambdaUpdateWrapper<Admin> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(admin.getPhone() != null && !"".equals(admin.getPhone()), Admin::getPhone, admin.getPhone())
                .set(admin.getPassword() != null && !"".equals(admin.getPassword()), Admin::getPassword, Md5Util.getMD5String(admin.getPassword()) )
                .set(admin.getName() != null && !"".equals(admin.getName()), Admin::getName, admin.getName())
                .set(admin.getMoney() != null , Admin::getMoney, admin.getMoney())
                .eq(Admin::getId, admin.getId());
        return adminMapper.update(wrapper) > 0;
    }

    @Override
    public boolean deleteById(int id) {
        return adminMapper.deleteById(id) > 0;
    }

}
